package com.example.base.validation.component;


import com.example.base.validation.annation.IsClass;
import com.example.base.validation.utils.ValidatedUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Classname IsClassValidator
 * @Description TODO
 * @Date 2023/1/14 19:46
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class IsClassValidator implements ConstraintValidator<IsClass, String> {

    private boolean required = false;

    @Override
    public void initialize(IsClass constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        System.out.println(value+"开始反射校验");
        // value为空时进行校验
        if (StringUtils.isBlank(value)) {
            if (required) {
                return false;
            }
            return true;
        } else {
            // 不为空时进行校验
            return ValidatedUtils.isClass(value);
        }
    }
}
